import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
//ElementUI
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
//路由
import VueRouter from 'vue-router'
import routes from './routes'

import store from './vuex/store'
import Vuex from 'vuex'

import axios from 'axios'
//配置axios的全局基本路径
axios.defaults.baseURL='http://localhost:8080/'
//全局属性配置，在任意组件内可以使用this.$http获取axios对象
Vue.prototype.$http = axios


 //import Mock from './mock'
//Mock.bootstrap();
import 'font-awesome/css/font-awesome.min.css'

Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)

//NProgress.configure({ showSpinner: false });

const router = new VueRouter({
  routes
})



//前端拦截器
router.beforeEach((to, from, next) => {
  //NProgress.start();
  if (to.path == '/login') {
    sessionStorage.removeItem('user');
  }
  let user = JSON.parse(sessionStorage.getItem('user'));
  if (!user && to.path != '/login') {
    next({ path: '/login' })
  } else {
    next()
  }
})

//前置拦截器
//使用axios前置拦截器，让所有的请求都携带Token
axios.interceptors.request.use(config=>{
  //携带token
  let token =  sessionStorage.getItem("token");
  if(token){
    config.headers["Token"]=token;
  }
  return config;
},error => {
  Promise.reject(error);
})

//后置拦截器
router.afterEach(transition => {
NProgress.done();
});

new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')

